package com.github.binarywang.demo.wx.miniapp.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器
 */
@Component
@Slf4j
public class AppIntercepter implements HandlerInterceptor {


    /**
     * 进入controller层之前拦截请求
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (!handler.getClass().isAssignableFrom(HandlerMethod.class)) {
            return true;
        }
        log.info("拦截器-----打印日志------");

        return true;
        // String token = request.getHeader(Const.TOKEN_NAME);
        // if (StrUtil.isBlank(token)) {
        //     token = request.getParameter(Const.TOKEN_NAME);
        // }
        //
        // if (StrUtil.isBlank(token)) {
        //     ResponseUtil.write(response, JacksonUtils.obj2json(RespResult.fail(ResponseCode.VALID_TOKEN_ERROR)));
        //     return false;
        // } else {
        //
        //     //判断token是否合法
        //
        //     //将组装好的用户信息放到req域
        //     // request.setAttribute(ParkConst.REQ_ATTRIBUTE_NAME, currentUser);
        //     ResponseUtil.write(response, JacksonUtils.obj2json(RespResult.fail(ResponseCode.VALID_TOKEN_ERROR)));
        //     return false;
        // }
    }

    /**
     * 处理请求完成后视图渲染之前的处理操作
     *
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    /**
     * 视图渲染之后的操作
     *
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }


}
